package chatclient.applets.note;

import chatclient.tools.NoteContainer;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Enumeration;
import java.util.Vector;

public class LinePart implements NotePart {

    public LinePart(int i) {
        maxWidth = i;
        width = 0;
        eol = false;
        whiteSpace = true;
        leading = 0;
        ascent = 0;
        maxAscent = 0;
        descent = 0;
        maxDescent = 0;
        parts = new Vector();
    }

    public boolean add(NotePart notepart, boolean flag) {
        if (!flag && (eol || width + notepart.getWidth() > maxWidth)) {
            return false;
        }
        eol |= notepart.hasEol();
        whiteSpace &= notepart.isWhiteSpace();
        width += notepart.getWidth();
        if (ascent + leading < notepart.getAscent() + notepart.getLeading()) {
            leading = (notepart.getAscent() + notepart.getLeading()) - ascent;
        }
        if (ascent < notepart.getAscent()) {
            leading -= notepart.getAscent() - ascent;
            ascent = notepart.getAscent();
        }
        if (maxAscent < notepart.getMaxAscent()) {
            maxAscent = notepart.getMaxAscent();
        }
        if (descent < notepart.getDescent()) {
            descent = notepart.getDescent();
        }
        if (maxDescent < notepart.getMaxDescent()) {
            maxDescent = notepart.getMaxDescent();
        }
        parts.addElement(notepart);
        return true;
    }

    @Override
    public int getWidth() {
        return width;
    }

    @Override
    public boolean isWhiteSpace() {
        return whiteSpace;
    }

    @Override
    public boolean hasEol() {
        return eol;
    }

    @Override
    public int getAscent() {
        return ascent;
    }

    @Override
    public int getDescent() {
        return descent;
    }

    @Override
    public int getLeading() {
        return leading;
    }

    @Override
    public int getMaxAscent() {
        return maxAscent;
    }

    @Override
    public int getMaxDescent() {
        return maxDescent;
    }

    @Override
    public void paint(NoteContainer notecontainer, Note note, Graphics g, int i, int j, String s, Color color,
            Double double1) {
        for (Enumeration enumeration = parts.elements(); enumeration.hasMoreElements();) {
            NotePart notepart = (NotePart) enumeration.nextElement();
            notepart.paint(notecontainer, note, g, i, (j + (leading + ascent)) - (notepart.getLeading() + notepart.getAscent()), s, color, double1);
            i += notepart.getWidth();
        }

    }

    @Override
    public String toString() {
        String s = "";
        for (Enumeration enumeration = parts.elements(); enumeration.hasMoreElements();) {
            s = (new StringBuilder(String.valueOf(s))).append(enumeration.nextElement().toString()).toString();
        }

        return s;
    }
    private int maxWidth;
    private int width;
    private boolean eol;
    private boolean whiteSpace;
    private int leading;
    private int ascent;
    private int maxAscent;
    private int descent;
    private int maxDescent;
    private Vector parts;
    

    public NotePart breakAtWidth(int i, boolean flag, LinePart linepart) {
        if (flag) {
            linepart.add(this, true);
            return null;
        } else {
            return this;
        }
    }

    public int getHeight() {
        return getLeading() + getAscent() + getDescent();
    }


}
